package com.pengji.rcp.plugin;

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import com.pengji.rcp.plugin.login.dialog.LoginInDialog;
import com.pengji.rcp.plugin.login.dialog.SuperContext;

public class Application implements IApplication {

	@Override
	public Object start(IApplicationContext context) {
		Display display = PlatformUI.createDisplay();
		SuperContext superContext=SuperContext.getInstance();
		try {
			if(!login(superContext))
				return Application.EXIT_OK;
			int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
			if (returnCode == PlatformUI.RETURN_RESTART) {
				return IApplication.EXIT_RESTART;
			}
			
			return IApplication.EXIT_OK;
		} finally {
			display.dispose();
		}
	}

	@Override
	public void stop() {
		if (!PlatformUI.isWorkbenchRunning())
			return;
		final IWorkbench workbench = PlatformUI.getWorkbench();
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable() {
			@Override
			public void run() {
				if (!display.isDisposed())
					workbench.close();
			}
		});
	}
	
	
	private boolean login(SuperContext superContext){
		//LoginInDialog dialog=new LoginInDialog(null);
		//if(dialog.open()==Window.OK){
			superContext.setLogin(true);
		//}
		return superContext.isLogin();
	}
}
